package com.mission.schedule.calendar;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class CalendarView extends ViewGroup implements CalendarTopView {

    private int selectPostion = -1;

    private CaledarAdapter adapter;
    private List<CalendarBean> data;
    private OnItemClickListener onItemClickListener;

    private int row = 6;
    private int column = 7;
    private int itemWidth;
    private int itemHeight;

    private boolean isToday;

    private boolean isMove = false;
    private boolean isCutOff = true;
    private float sx, sy;
    private Handler handler;

    public interface OnItemClickListener {
        void onItemClick(View view, int postion, CalendarBean bean);

        void onLongItemClick(View view, int postion, CalendarBean bean);
    }

    public CalendarView(Context context, int row) {
        super(context);
        this.row = row;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int[] getCurrentSelectPositon() {
        return new int[0];
    }

    public int getItemHeight() {
        return itemHeight;
    }

    @Override
    public void setCaledarTopViewChangeListener(CaledarTopViewChangeListener listener) {

    }

    @Override
    public void notifyDataSetChanged() {

    }

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public void setAdapter(CaledarAdapter adapter) {
        this.adapter = adapter;
    }

    public void setData(List<CalendarBean> data, boolean isToday) {
        this.data = data;
        this.isToday = isToday;
        setItem();
        requestLayout();
    }

    private void setItem() {

        selectPostion = -1;
        if (adapter == null) {
            throw new RuntimeException("adapter is null,please setadapter");
        }

        for (int i = 0; i < data.size(); i++) {

            CalendarBean bean = data.get(i);
            View view = getChildAt(i);

            View chidView = adapter.getView(view, this, bean);

            if (view == null || view != chidView) {
                addViewInLayout(chidView, i, chidView.getLayoutParams(), true);
            }

            if (isToday && selectPostion == -1) {
                int[] date = CalendarUtil.getYMD(new Date());
                if (bean.year == date[0] && bean.moth == date[1] && bean.day == date[2]) {
                    selectPostion = i;
                }
            } else {
                if (selectPostion == -1 && bean.day == 1) {
                    selectPostion = i;
                }
            }

            chidView.setSelected(selectPostion == i);

            setItemClick(chidView, i, bean);

        }
    }

    public Object[] getSelect() {
        return new Object[]{getChildAt(selectPostion), selectPostion, data.get(selectPostion)};
    }

    public void setItemClick(final View view, final int potsion, final CalendarBean bean) {
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (selectPostion != -1) {
                    getChildAt(selectPostion).setSelected(false);
                    getChildAt(potsion).setSelected(true);
                }
                selectPostion = potsion;

                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(view, potsion, bean);
                }
            }
        });

        view.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onLongItemClick(view, potsion, bean);
                }
                return true;
            }
        });
    }

    public int[] getSelectPostion() {
        Rect rect = new Rect();
        try {
            getChildAt(selectPostion).getHitRect(rect);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new int[]{rect.left, rect.top, rect.right, rect.bottom};
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int parentWidth = MeasureSpec.getSize(MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.EXACTLY));

        itemWidth = parentWidth / column;
        itemHeight = MeasureSpec.getSize(heightMeasureSpec) / row;

        View view = getChildAt(0);

        if (view == null) {
            return;
        }
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params != null && params.height > 0) {
            itemHeight = params.height;
        }
        setMeasuredDimension(parentWidth, itemHeight * row);


        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            childView.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            layoutChild(getChildAt(i), i, l, t, r, b);
        }
    }

    private void layoutChild(View view, int postion, int l, int t, int r, int b) {

        int cc = postion % column;
        int cr = postion / column;

        int itemWidth = view.getMeasuredWidth();
        int itemHeight = view.getMeasuredHeight();

        l = cc * itemWidth;
        t = cr * itemHeight;
        r = l + itemWidth;
        b = t + itemHeight;
        view.layout(l, t, r, b);

    }

    public void setHandler(Handler h) {
        handler = h;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        final int action = ev.getActionMasked();

        if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
            // 该事件可能不是我们的
            return false;
        }

        boolean isIntercept = false;
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                sx=ev.getX();
                sy=ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float tx = ev.getX();
                float ty = ev.getY();
                float rx = tx - sx;
                float ry = ty - sy;
                if (Math.abs(rx) != Math.abs(ry)) {
                    isIntercept = true;
                }
                if (isIntercept) {
                    sx = tx;
                    sy = ty;
                }
                break;
        }
        return isIntercept;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        switch (ev.getAction()) {

            case MotionEvent.ACTION_UP:
                float tx = ev.getX();
                float ty = ev.getY();
                float rx = tx - sx;
                float ry = ty - sy;
                if (Math.abs(rx) > Math.abs(ry)) {
                    if (Math.abs(rx) > 20) {
                        isMove = true;
                    }
                } else {
                    if (Math.abs(ry) > 20) {
                        isMove = true;
                    }
                }
                if (isMove) {
                    isMove = false;
                    if (Math.abs(rx) > Math.abs(ry)) {
                        if (rx > 0) {
                            handler.sendEmptyMessage(0);
                        } else {
                            handler.sendEmptyMessage(1);
                        }
                    } else {
                        if (ry > 0) {
                            handler.sendEmptyMessage(2);
                        } else {
                            handler.sendEmptyMessage(3);
                        }
                    }
                } else {
                    isCutOff = false;
                    ev.setAction(MotionEvent.ACTION_DOWN);
                    dispatchTouchEvent(ev);
//                    ev.setAction(MotionEvent.ACTION_UP);
//                    dispatchTouchEvent(ev);
                    return false;
                }
                break;
            default:
                break;
        }
        return true;
    }

}